<?php
namespace app\common\logic\mqlogic;

use app\common\library\rabitmq\MqConsumerObject;
use app\common\logic\AudioLogic;
use app\common\model\broadcasting\Item;
use think\exception\ValidateException;

//进行视频转码。
class RadioTransferCodeConsumer extends MqConsumerObject
{
    public function __construct(array $conf = [])
    {
        if (!$conf) {
            $conf = config('site.rabitmqconfig');
        }
        parent::__construct($conf);
    }

    //消费者处理类
    public function run($message)
    {
        $message = json_decode($message, true);
        $id = $message['id']; //电台节目id

        try {
            $broadcast = Item::get($id);
            if (!$broadcast) {
                throw new ValidateException($id . '的视频源不存在');
            }

            //进行视频转码
            $audio = new AudioLogic(ROOT_PATH . 'public' . $broadcast['src']);
            $format = $audio->getFormatFromAudio();  //获取编码
            $audio->setFormat($format)->clip()->transferCode();      //进行切片和转码

            //修改视频状态
            $broadcast->is_transcoding = 1;
            $broadcast->transcoding = $audio->getUploadsPath('transcoding');
            $broadcast->transcoding_clip = $audio->getUploadsPath('clip');

            $broadcast->save();
        }catch (\Exception $e) {
            $msg = $e->getMessage();
            doLog($msg, 'RadioTransferCode');
        }

        return true;
    }

    public function __destruct()
    {
        parent::__destruct();
    }

}